package niuniu

import (
	"math/rand"
	"strconv"
	"strings"
	"time"
)

var NiuNiuPaiDianShu = map[Pai]int{
	`hei_1`:  1,
	`hei_2`:  2,
	`hei_3`:  3,
	`hei_4`:  4,
	`hei_5`:  5,
	`hei_6`:  6,
	`hei_7`:  7,
	`hei_8`:  8,
	`hei_9`:  9,
	`hei_10`: 10,
	`hei_11`: 10,
	`hei_12`: 10,
	`hei_13`: 10,

	`hong_1`:  1,
	`hong_2`:  2,
	`hong_3`:  3,
	`hong_4`:  4,
	`hong_5`:  5,
	`hong_6`:  6,
	`hong_7`:  7,
	`hong_8`:  8,
	`hong_9`:  9,
	`hong_10`: 10,
	`hong_11`: 10,
	`hong_12`: 10,
	`hong_13`: 10,

	`mei_1`:  1,
	`mei_2`:  2,
	`mei_3`:  3,
	`mei_4`:  4,
	`mei_5`:  5,
	`mei_6`:  6,
	`mei_7`:  7,
	`mei_8`:  8,
	`mei_9`:  9,
	`mei_10`: 10,
	`mei_11`: 10,
	`mei_12`: 10,
	`mei_13`: 10,

	`fang_1`:  1,
	`fang_2`:  2,
	`fang_3`:  3,
	`fang_4`:  4,
	`fang_5`:  5,
	`fang_6`:  6,
	`fang_7`:  7,
	`fang_8`:  8,
	`fang_9`:  9,
	`fang_10`: 10,
	`fang_11`: 10,
	`fang_12`: 10,
	`fang_13`: 10,
}

var NiuNiuPai = map[Pai]string{
	`hei_1`:   `黑桃A`,
	`hei_2`:   `黑桃2`,
	`hei_3`:   `黑桃3`,
	`hei_4`:   `黑桃4`,
	`hei_5`:   `黑桃5`,
	`hei_6`:   `黑桃6`,
	`hei_7`:   `黑桃7`,
	`hei_8`:   `黑桃8`,
	`hei_9`:   `黑桃9`,
	`hei_10`:  `黑桃10`,
	`hei_11`:  `黑桃J`,
	`hei_12`:  `黑桃Q`,
	`hei_13`:  `黑桃K`,
	`hong_1`:  `红桃A`,
	`hong_2`:  `红桃2`,
	`hong_3`:  `红桃3`,
	`hong_4`:  `红桃4`,
	`hong_5`:  `红桃5`,
	`hong_6`:  `红桃6`,
	`hong_7`:  `红桃7`,
	`hong_8`:  `红桃8`,
	`hong_9`:  `红桃9`,
	`hong_10`: `红桃10`,
	`hong_11`: `红桃J`,
	`hong_12`: `红桃Q`,
	`hong_13`: `红桃K`,
	`mei_1`:   `梅花A`,
	`mei_2`:   `梅花2`,
	`mei_3`:   `梅花3`,
	`mei_4`:   `梅花4`,
	`mei_5`:   `梅花5`,
	`mei_6`:   `梅花6`,
	`mei_7`:   `梅花7`,
	`mei_8`:   `梅花8`,
	`mei_9`:   `梅花9`,
	`mei_10`:  `梅花10`,
	`mei_11`:  `梅花J`,
	`mei_12`:  `梅花Q`,
	`mei_13`:  `梅花K`,
	`fang_1`:  `方块A`,
	`fang_2`:  `方块2`,
	`fang_3`:  `方块3`,
	`fang_4`:  `方块4`,
	`fang_5`:  `方块5`,
	`fang_6`:  `方块6`,
	`fang_7`:  `方块7`,
	`fang_8`:  `方块8`,
	`fang_9`:  `方块9`,
	`fang_10`: `方块10`,
	`fang_11`: `方块J`,
	`fang_12`: `方块Q`,
	`fang_13`: `方块K`,
}

const (
	MeiNiu = iota
	Niu1
	Niu2
	Niu3
	Niu4
	Niu5
	Niu6
	Niu7
	Niu8
	Niu9
	NiuNiu10
	YinNiu11
	JinNiu12
)

var NiuNRateMap = map[int]int{
	MeiNiu:   1,
	Niu1:     1,
	Niu2:     1,
	Niu3:     1,
	Niu4:     1,
	Niu5:     1,
	Niu6:     1,
	Niu7:     2,
	Niu8:     2,
	Niu9:     2,
	NiuNiu10: 3,
	YinNiu11: 4,
	JinNiu12: 5,
}

var HuaSeMap = map[string]int{
	"fang": 0,
	"mei":  1,
	"hong": 2,
	"hei":  3,
}

type Pai string

func NewPai(s string) Pai {
	return Pai(s)
}

func (p Pai) Text() string {
	s, _ := NiuNiuPai[p]
	return s
}

func (p Pai) Value() int {
	v, _ := NiuNiuPaiDianShu[p]
	return v
}

func (p Pai) No() int {
	s := strings.Split(string(p), "_")
	i, _ := strconv.Atoi(s[1])
	return i
}

func (p Pai) IsHua() bool {
	s := strings.Split(string(p), "_")
	if s[1] == "11" || s[1] == "12" || s[1] == "13" {
		return true
	}
	return false
}

func (p Pai) HuaSeVal() int {
	return HuaSeMap[p.HuaSe()]
}

func (p Pai) HuaSe() string {
	s := strings.Split(string(p), "_")
	return s[0]
}

func RandomGenPai(data map[Pai]string, n int) map[Pai]string {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	var keys []Pai
	for key, _ := range data {
		keys = append(keys, key)
	}
	keyRandomIndexs := r.Perm(len(keys))
	var ret = make(map[Pai]string)
	for i := 0; i < n; i++ {
		key := keys[keyRandomIndexs[i]]
		ret[key] = data[key]
		delete(data, key)
	}
	return ret
}

func NewPaisFromStringSlice(ss []string) []Pai {
	var retval []Pai
	for _, s := range ss {
		retval = append(retval, Pai(s))
	}
	return retval
}

func PaisMaping(pais []Pai) map[Pai]string {
	var ret = make(map[Pai]string)
	for _, p := range pais {
		ret[p] = p.Text()
	}
	return ret
}

func PaisMapToKeys(d map[Pai]string) []Pai {
	var pais []Pai
	for p, _ := range d {
		pais = append(pais, p)
	}
	return pais
}

func PaisMapToStringKeys(d map[Pai]string) []string {
	var pais []string
	for p, _ := range d {
		pais = append(pais, string(p))
	}
	return pais
}

func PaisNoMax(data []Pai) Pai {
	var max = data[0]
	for _, p := range data {
		if p.No() > max.No() {
			max = p
		}
	}
	return max
}
